package gwt.bbsbot.client;

import gwt.bbsbot.client.widget.BbsWidget;
import gwt.bbsbot.client.widget.TestBbsWidget;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GwtBBSBot implements EntryPoint, SelectionHandler<Integer> {
	
	//タブパネルの数とか定数
	private static final int BBS_MAX_NUM = 2;
	
	//Debug
	private Panel panelServerRes;
	private Label serverResponseLabel;
	private HTML htmlError;
	
	private BbsWidget[] bbsWidgets;
	
	private TabPanel tp = new TabPanel();
	private TestBbsWidget testBbs;
	private TestBbsWidget testBbs2;
	
	//mention
	private Panel panelMention;
	private HTML htmlMention;
	private Button btGetMention;
	private AsyncCallback<List<String>> callbackGetMention;

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
//		System.out.println(Location.getHost());
		this.bbsWidgets = new BbsWidget[BBS_MAX_NUM];
		this.testBbs = new TestBbsWidget();
		this.testBbs.setTitle("プログラミングのくだらない質問はここで聞け  テスト版");
		this.testBbs2 = new TestBbsWidget();
		this.testBbs2.setTitle("Test2");
		
		this.bbsWidgets[0] = this.testBbs;
		this.bbsWidgets[1] = this.testBbs2;
		//BBSメインタブを表示
		this.tp.add(this.bbsWidgets[0],"test");
		this.tp.add(this.bbsWidgets[1],"test2");
		this.tp.addSelectionHandler(this);
		this.tp.selectTab(0);
		
		createDebugForm();
		
		
		createSearchForm();
		
		//RootPanelに追加
		RootPanel.get("error").add(panelServerRes);
		RootPanel.get("tabpanel").add(this.tp);
	}
	
	
	/**
	 * デバッグフォームを作成する
	 */
	private void createDebugForm(){
		this.htmlError = new HTML();
		this.serverResponseLabel = new Label();
		this.panelServerRes = new VerticalPanel();
		this.panelServerRes.add(this.htmlError);
		this.panelServerRes.add(this.serverResponseLabel);
	}
	
	
	
	/**
	 * 検索フォームを作成する
	 */
	private void createSearchForm(){
		//検索
		this.panelMention = new VerticalPanel();
		this.htmlMention = new HTML();
		this.btGetMention = new Button("GetMention");
		this.panelMention.add(this.btGetMention);
		this.panelMention.add(this.htmlMention);
		this.tp.add(this.panelMention, "GetMentions");
		
		//検索コールバック
		this.callbackGetMention = new AsyncCallback<List<String>>(){
			@Override
			public void onSuccess(List<String> result) {
				StringBuilder sb = new StringBuilder();
				for(String val : result){
					sb.append(val);
					sb.append("<br>");
				}
				htmlMention.setHTML(sb.toString());
			}

			@Override
			public void onFailure(Throwable caught) {
				htmlError.setHTML(caught.getMessage());
			}
		};

		//探すRPC
		this.btGetMention.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				greetingService.getMentions(callbackGetMention);
			}
		});
	}


	@Override
	public void onSelection(SelectionEvent<Integer> event) {
		if(event.getSource() == this.tp){
			int select = event.getSelectedItem();
			if(select < BBS_MAX_NUM ){
				this.bbsWidgets[event.getSelectedItem()].displayBBS();
			}
		}
	}
}
